/*
 * StateMachineControl.java
 *
 * Created on 18 March 2004, 09:25
 * Copyright (C) 2004, Velocityme Partnership
 */

package com.velocityme.client.gui.node.taskstatemachine;

import com.velocityme.client.gui.exception.ExceptionWindow;
import com.velocityme.client.utility.StatelessRemoteClientSessionUtil;
import com.velocityme.interfaces.KeySession;
import com.velocityme.interfaces.TaskStateMachinePK;
import com.velocityme.utility.InvalidKeyException;
import com.velocityme.utility.PermissionDeniedException;
import com.velocityme.valueobjects.RoleValue;
import com.velocityme.valueobjects.StateValue;
import com.velocityme.valueobjects.StatusValue;
import com.velocityme.valueobjects.TaskStateMachineValue;
import com.velocityme.valueobjects.TransitionValue;
import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import javax.ejb.CreateException;
import javax.naming.NamingException;
import javax.swing.JOptionPane;

/**
 *
 * @author  Robert
 */
public class StateMachineControl extends javax.swing.JPanel {
    
    private TaskStateMachinePK m_taskStateMachinePK;
    private TaskStateMachineValue m_taskStateMachineValue;
    private KeySession m_key;
    private StateMachineValueDisplay m_stateMachineValueDisplay;

    /** Creates new form StateMachineControl */
    public StateMachineControl(KeySession p_key, TaskStateMachinePK p_taskStateMachinePK) {
        initComponents();
        m_key = p_key;
        m_taskStateMachinePK = p_taskStateMachinePK;
        try {
            m_taskStateMachineValue = StatelessRemoteClientSessionUtil.getSession().getTaskStateMachineValue(m_key, m_taskStateMachinePK);
            StateValue startStateValue = StatelessRemoteClientSessionUtil.getSession().getStartStateValue(m_key);
            StateValue[] allStateValues = StatelessRemoteClientSessionUtil.getSession().getAllStateValues(m_key);
            StatusValue[] allStatusValues = StatelessRemoteClientSessionUtil.getSession().getAllStatusValues(m_key);
            RoleValue[] allRoleValues = StatelessRemoteClientSessionUtil.getSession().getAllRoleValues(m_key);
            m_stateMachineValueDisplay = new StateMachineValueDisplay(m_taskStateMachineValue.getTransitionValues(), startStateValue, allStateValues, allStatusValues, allRoleValues);
            this.add(m_stateMachineValueDisplay, java.awt.BorderLayout.CENTER);
        }
        catch(RemoteException e) {
            ExceptionWindow.showError(e);
        }
        catch(NamingException e) {
            ExceptionWindow.showError(e);
        }
        catch(CreateException e) {
            ExceptionWindow.showError(e);
        }
        catch(InvalidKeyException e) {
            ExceptionWindow.showError(e);
        }
        catch(PermissionDeniedException e) {
            JOptionPane.showMessageDialog(this, "Permission denied!", "View State Machine", JOptionPane.WARNING_MESSAGE);
        }
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    private void initComponents() {//GEN-BEGIN:initComponents
        jPanel2 = new javax.swing.JPanel();
        jButtonOK = new javax.swing.JButton();
        jButtonCancel = new javax.swing.JButton();

        setLayout(new java.awt.BorderLayout());

        jPanel2.setLayout(new java.awt.GridLayout(1, 0));

        jButtonOK.setText("OK");
        jButtonOK.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonOKActionPerformed(evt);
            }
        });

        jPanel2.add(jButtonOK);

        jButtonCancel.setText("Cancel");
        jButtonCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonCancelActionPerformed(evt);
            }
        });

        jPanel2.add(jButtonCancel);

        add(jPanel2, java.awt.BorderLayout.SOUTH);

    }//GEN-END:initComponents

    private void jButtonCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonCancelActionPerformed
        // Add your handling code here:
        getParent().remove(this);                        
    }//GEN-LAST:event_jButtonCancelActionPerformed

    private void jButtonOKActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonOKActionPerformed
        // Add your handling code here:
        TransitionValue[] newTransitionValues = m_stateMachineValueDisplay.getTransitionValueObjects();
        m_taskStateMachineValue.setTransitionValues(newTransitionValues);
        try {
            StatelessRemoteClientSessionUtil.getSession().taskStateMachineEdit(m_key, m_taskStateMachinePK, m_taskStateMachineValue);
            getParent().remove(this);
        }
        catch(RemoteException e) {
            ExceptionWindow.showError(e);
        }
        catch(NamingException e) {
            ExceptionWindow.showError(e);
        }
        catch(CreateException e) {
            ExceptionWindow.showError(e);
        }
        catch(PermissionDeniedException e) {
            ExceptionWindow.showError(e);
        }
        catch(InvalidKeyException e) {
            ExceptionWindow.showError(e);
        }        
    }//GEN-LAST:event_jButtonOKActionPerformed
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonCancel;
    private javax.swing.JButton jButtonOK;
    private javax.swing.JPanel jPanel2;
    // End of variables declaration//GEN-END:variables
    
}
